package frontend.registration.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;

import core.discount.model.Coupon;
import core.util.HibernateCoreUtil;

public class DiscountManager extends HibernateCoreUtil
{
	public Map<String,String> getDiscountByCode(String discountCode)
	{
		Map<String,String> result = new HashMap<String,String>();
		Coupon c = null ;
		Float value = 0f ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Coupon> coupLst = session.createQuery("from Coupon where couponcode = '"+discountCode+"'").list();
		if(coupLst.size() == 0)
		{
			result.put("Error","Coupon with this code does not exists");
		}
		else
		{
			c = coupLst.get(0);
			if(c.isUsed())
			{
				result.put("Error","Coupon with this code is used");
			}
			else
			{
				value = c.getAmount();
				result.put("discount",value.toString());
			}
		}
		session.getTransaction().commit();
		return result ;
	}
}
